<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>搜索 | Elasticsearch 权威指南</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../getting_started/tutorial_aggregations.html" />
    
    
    <link rel="prev" href="../getting_started/tutorial_indexing.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="1.6" data-basepath=".." data-revision="1436390985808">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         Introduction
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="getting_started/README.html">
            
                
                    <a href="../getting_started/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         入门
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="1.1" data-path="getting_started/what_is_it.html">
            
                
                    <a href="../getting_started/what_is_it.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                         初识
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.2" data-path="getting_started/installing_es.html">
            
                
                    <a href="../getting_started/installing_es.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                         安装
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.3" data-path="getting_started/api.html">
            
                
                    <a href="../getting_started/api.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                         API
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.4" data-path="getting_started/document.html">
            
                
                    <a href="../getting_started/document.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                         文档
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.5" data-path="getting_started/tutorial_indexing.html">
            
                
                    <a href="../getting_started/tutorial_indexing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                         索引
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="1.6" data-path="getting_started/tutorial_search.html">
            
                
                    <a href="../getting_started/tutorial_search.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                         搜索
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.7" data-path="getting_started/tutorial_aggregations.html">
            
                
                    <a href="../getting_started/tutorial_aggregations.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                         汇总
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.8" data-path="getting_started/tutorial_conclusion.html">
            
                
                    <a href="../getting_started/tutorial_conclusion.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                         小结
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.9" data-path="getting_started/distributed.html">
            
                
                    <a href="../getting_started/distributed.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.9.</b>
                        
                         分布式
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.10" data-path="getting_started/conclusion.html">
            
                
                    <a href="../getting_started/conclusion.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.10.</b>
                        
                         本章总结
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="distributed_cluster/README.html">
            
                
                    <a href="../distributed_cluster/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         分布式集群
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="2.1" data-path="distributed_cluster/empty_cluster.html">
            
                
                    <a href="../distributed_cluster/empty_cluster.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                         空集群
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.2" data-path="distributed_cluster/cluster_health.html">
            
                
                    <a href="../distributed_cluster/cluster_health.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                         集群健康
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.3" data-path="distributed_cluster/add_an_index.html">
            
                
                    <a href="../distributed_cluster/add_an_index.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                         添加索引
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.4" data-path="distributed_cluster/add_failover.html">
            
                
                    <a href="../distributed_cluster/add_failover.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                         容错移转
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.5" data-path="distributed_cluster/scale_horizontally.html">
            
                
                    <a href="../distributed_cluster/scale_horizontally.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                         横向扩展
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.6" data-path="distributed_cluster/scale_more.html">
            
                
                    <a href="../distributed_cluster/scale_more.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                         扩展
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.7" data-path="distributed_cluster/coping_with_failure.html">
            
                
                    <a href="../distributed_cluster/coping_with_failure.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                         故障恢复
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="data/README.html">
            
                
                    <a href="../data/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         数据
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="3.1" data-path="data/document.html">
            
                
                    <a href="../data/document.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                         文档
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.2" data-path="data/index.html">
            
                
                    <a href="../data/index.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                         索引
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.3" data-path="data/get.html">
            
                
                    <a href="../data/get.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                         Get
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.4" data-path="data/exists.html">
            
                
                    <a href="../data/exists.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                         存在
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.5" data-path="data/update.html">
            
                
                    <a href="../data/update.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                         更新
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.6" data-path="data/create.html">
            
                
                    <a href="../data/create.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                         创建
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.7" data-path="data/delete.html">
            
                
                    <a href="../data/delete.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                         删除
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.8" data-path="data/version_control.html">
            
                
                    <a href="../data/version_control.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                         版本控制
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.9" data-path="data/partial_update.html">
            
                
                    <a href="../data/partial_update.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                         局部更新
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.10" data-path="data/mget.html">
            
                
                    <a href="../data/mget.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                         Mget
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.11" data-path="data/bulk.html">
            
                
                    <a href="../data/bulk.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.11.</b>
                        
                         Bulk
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.12" data-path="data/conclusion.html">
            
                
                    <a href="../data/conclusion.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.12.</b>
                        
                         总结
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="distributed_crud/README.html">
            
                
                    <a href="../distributed_crud/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         分布式文档存储
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="4.1" data-path="distributed_crud/routing.html">
            
                
                    <a href="../distributed_crud/routing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                         路由
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.2" data-path="distributed_crud/shard_interaction.html">
            
                
                    <a href="../distributed_crud/shard_interaction.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                         主从互通
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.3" data-path="distributed_crud/create_index_delete.html">
            
                
                    <a href="../distributed_crud/create_index_delete.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                         创建索引删除
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.4" data-path="distributed_crud/retrieving.html">
            
                
                    <a href="../distributed_crud/retrieving.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                         获取
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.5" data-path="distributed_crud/partial_updates.html">
            
                
                    <a href="../distributed_crud/partial_updates.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                         局部更新
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.6" data-path="distributed_crud/bulk_requests.html">
            
                
                    <a href="../distributed_crud/bulk_requests.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                         批量请求
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.7" data-path="distributed_crud/bulk_format.html">
            
                
                    <a href="../distributed_crud/bulk_format.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                         批量格式
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="search/README.html">
            
                
                    <a href="../search/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         搜索
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="5.1" data-path="search/empty_search.html">
            
                
                    <a href="../search/empty_search.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                         空白搜索
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.2" data-path="search/multi_index_multi_type.html">
            
                
                    <a href="../search/multi_index_multi_type.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                         多索引多类型
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.3" data-path="search/pagination.html">
            
                
                    <a href="../search/pagination.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                         分页
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.4" data-path="search/query_string.html">
            
                
                    <a href="../search/query_string.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                         查询语句
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="mapping_analysis/README.html">
            
                
                    <a href="../mapping_analysis/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         映射与统计
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="6.1" data-path="mapping_analysis/exact_vs_full_text.html">
            
                
                    <a href="../mapping_analysis/exact_vs_full_text.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                         Exact_vs_full_text
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.2" data-path="mapping_analysis/inverted_index.html">
            
                
                    <a href="../mapping_analysis/inverted_index.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                         Inverted_index
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.3" data-path="mapping_analysis/analysis.html">
            
                
                    <a href="../mapping_analysis/analysis.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                         Analysis
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.4" data-path="mapping_analysis/mapping.html">
            
                
                    <a href="../mapping_analysis/mapping.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                         Mapping
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.5" data-path="mapping_analysis/complex_datatypes.html">
            
                
                    <a href="../mapping_analysis/complex_datatypes.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                         Complex_datatypes
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Elasticsearch 权威指南</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_897">
                    
                        <h1 id="">检索文档</h1>
<p>现在，我们已经在 Elasticsearch 中存储了一些数据，我们可以开始根据这个项目的需求进行工作了。第一个需求就是要能搜索每一个员工的数据。</p>
<p>对于 Elasticsearch 来说，这是非常简单的。我们只需要执行一次 HTTP GET 请求，然后指出文档的<strong>地址</strong>，也就是索引、类型以及 ID 即可。通过这三个部分，我们就可以得到原始的 JSON 文档：</p>
<pre><code class="lang-js">GET /megacorp/employee/<span class="hljs-number">1</span>
</code></pre>
<p>返回的内容包含了这个文档的元数据信息，而 John Smith 的原始 JSON 文档也在 <code>_source</code> 字段中出现了：</p>
<pre><code class="lang-js">{
  <span class="hljs-string">"_index"</span> :   <span class="hljs-string">"megacorp"</span>,
  <span class="hljs-string">"_type"</span> :    <span class="hljs-string">"employee"</span>,
  <span class="hljs-string">"_id"</span> :      <span class="hljs-string">"1"</span>,
  <span class="hljs-string">"_version"</span> : <span class="hljs-number">1</span>,
  <span class="hljs-string">"found"</span> :    <span class="hljs-literal">true</span>,
  <span class="hljs-string">"_source"</span> :  {
      <span class="hljs-string">"first_name"</span> :  <span class="hljs-string">"John"</span>,
      <span class="hljs-string">"last_name"</span> :   <span class="hljs-string">"Smith"</span>,
      <span class="hljs-string">"age"</span> :         <span class="hljs-number">25</span>,
      <span class="hljs-string">"about"</span> :       <span class="hljs-string">"I love to go rock climbing"</span>,
      <span class="hljs-string">"interests"</span>:  [ <span class="hljs-string">"sports"</span>, <span class="hljs-string">"music"</span> ]
  }
}
</code></pre>
<hr>
<p>我们通过将HTTP后的请求方式由 <code>PUT</code> 改变为 <code>GET</code> 来获取文档，同理，我们也可以将其更换为 <code>DELETE</code> 来删除这个文档，<code>HEAD</code> 是用来查询这个文档是否存在的。如果你想替换一个已经存在的文档，你只需要使用 <code>PUT</code> 再次发出请求即可。</p>
<hr>
<h1 id="">简易搜索</h1>
<p><code>GET</code> 命令真的相当简单，你只需要告诉它你要什么即可。接下来，我们来试一下稍微复杂一点的搜索。</p>
<p>我们首先要完成一个最简单的搜索命令来搜索全部员工：</p>
<pre><code class="lang-js">GET /megacorp/employee/_search
</code></pre>
<p>你可以发现我们正在使用 <code>megacorp</code> 索引，<code>employee</code> 类型，但是我们我们并没有指定文档的ID，我们现在使用的是 <code>_search</code> 端口。你可以再返回的 <code>hits</code> 中发现我们录入的三个文档。搜索会默认返回最前的10个数值。</p>
<pre><code class="lang-js">{
   <span class="hljs-string">"took"</span>:      <span class="hljs-number">6</span>,
   <span class="hljs-string">"timed_out"</span>: <span class="hljs-literal">false</span>,
   <span class="hljs-string">"_shards"</span>: { ... },
   <span class="hljs-string">"hits"</span>: {
      <span class="hljs-string">"total"</span>:      <span class="hljs-number">3</span>,
      <span class="hljs-string">"max_score"</span>:  <span class="hljs-number">1</span>,
      <span class="hljs-string">"hits"</span>: [
         {
            <span class="hljs-string">"_index"</span>:         <span class="hljs-string">"megacorp"</span>,
            <span class="hljs-string">"_type"</span>:          <span class="hljs-string">"employee"</span>,
            <span class="hljs-string">"_id"</span>:            <span class="hljs-string">"3"</span>,
            <span class="hljs-string">"_score"</span>:         <span class="hljs-number">1</span>,
            <span class="hljs-string">"_source"</span>: {
               <span class="hljs-string">"first_name"</span>:  <span class="hljs-string">"Douglas"</span>,
               <span class="hljs-string">"last_name"</span>:   <span class="hljs-string">"Fir"</span>,
               <span class="hljs-string">"age"</span>:         <span class="hljs-number">35</span>,
               <span class="hljs-string">"about"</span>:       <span class="hljs-string">"I like to build cabinets"</span>,
               <span class="hljs-string">"interests"</span>: [ <span class="hljs-string">"forestry"</span> ]
            }
         },
         {
            <span class="hljs-string">"_index"</span>:         <span class="hljs-string">"megacorp"</span>,
            <span class="hljs-string">"_type"</span>:          <span class="hljs-string">"employee"</span>,
            <span class="hljs-string">"_id"</span>:            <span class="hljs-string">"1"</span>,
            <span class="hljs-string">"_score"</span>:         <span class="hljs-number">1</span>,
            <span class="hljs-string">"_source"</span>: {
               <span class="hljs-string">"first_name"</span>:  <span class="hljs-string">"John"</span>,
               <span class="hljs-string">"last_name"</span>:   <span class="hljs-string">"Smith"</span>,
               <span class="hljs-string">"age"</span>:         <span class="hljs-number">25</span>,
               <span class="hljs-string">"about"</span>:       <span class="hljs-string">"I love to go rock climbing"</span>,
               <span class="hljs-string">"interests"</span>: [ <span class="hljs-string">"sports"</span>, <span class="hljs-string">"music"</span> ]
            }
         },
         {
            <span class="hljs-string">"_index"</span>:         <span class="hljs-string">"megacorp"</span>,
            <span class="hljs-string">"_type"</span>:          <span class="hljs-string">"employee"</span>,
            <span class="hljs-string">"_id"</span>:            <span class="hljs-string">"2"</span>,
            <span class="hljs-string">"_score"</span>:         <span class="hljs-number">1</span>,
            <span class="hljs-string">"_source"</span>: {
               <span class="hljs-string">"first_name"</span>:  <span class="hljs-string">"Jane"</span>,
               <span class="hljs-string">"last_name"</span>:   <span class="hljs-string">"Smith"</span>,
               <span class="hljs-string">"age"</span>:         <span class="hljs-number">32</span>,
               <span class="hljs-string">"about"</span>:       <span class="hljs-string">"I like to collect rock albums"</span>,
               <span class="hljs-string">"interests"</span>: [ <span class="hljs-string">"music"</span> ]
            }
         }
      ]
   }
}
</code></pre>
<p>注意：反馈值中不仅会告诉你匹配到哪些文档，同时也会把这个文档都会包含到其中：我们需要搜索的用户的所有信息。</p>
<p>接下来，我们将要尝试着实现搜索一下哪些员工的姓氏中包含 <code>Smith</code>。为了实现这个，我们需要使用一种<strong>轻量</strong>的搜索方法。这种方法经常被称做 <em>查询字符串(query string)</em> 搜索，因为我们通过URL来传递查询的关键字：</p>
<pre><code class="lang-js">GET /megacorp/employee/_search?q=last_name:Smith
</code></pre>
<p>我们依旧使用 <code>_search</code> 端口，然后可以将参数传入给 <code>q=</code>。这样我们就可以得到姓Smith的结果：</p>
<pre><code class="lang-js">{
   ...
   <span class="hljs-string">"hits"</span>: {
      <span class="hljs-string">"total"</span>:      <span class="hljs-number">2</span>,
      <span class="hljs-string">"max_score"</span>:  <span class="hljs-number">0.30685282</span>,
      <span class="hljs-string">"hits"</span>: [
         {
            ...
            <span class="hljs-string">"_source"</span>: {
               <span class="hljs-string">"first_name"</span>:  <span class="hljs-string">"John"</span>,
               <span class="hljs-string">"last_name"</span>:   <span class="hljs-string">"Smith"</span>,
               <span class="hljs-string">"age"</span>:         <span class="hljs-number">25</span>,
               <span class="hljs-string">"about"</span>:       <span class="hljs-string">"I love to go rock climbing"</span>,
               <span class="hljs-string">"interests"</span>: [ <span class="hljs-string">"sports"</span>, <span class="hljs-string">"music"</span> ]
            }
         },
         {
            ...
            <span class="hljs-string">"_source"</span>: {
               <span class="hljs-string">"first_name"</span>:  <span class="hljs-string">"Jane"</span>,
               <span class="hljs-string">"last_name"</span>:   <span class="hljs-string">"Smith"</span>,
               <span class="hljs-string">"age"</span>:         <span class="hljs-number">32</span>,
               <span class="hljs-string">"about"</span>:       <span class="hljs-string">"I like to collect rock albums"</span>,
               <span class="hljs-string">"interests"</span>: [ <span class="hljs-string">"music"</span> ]
            }
         }
      ]
   }
}
</code></pre>
<h1 id="query-dsl">使用Query DSL搜索</h1>
<p>查询字符串是通过命令语句完成 <em>点对点(ad hoc)</em> 的搜索，但是这也有它的局限性（可参阅《搜索局限性》章节）。Elasticsearch 提供了更加丰富灵活的查询语言，它被称作 <em>Query DSL</em>，通过它你可以完成更加复杂、强大的搜索任务。</p>
<p>DSL (<em>Domain Specific Language</em> 领域特定语言) 需要使用 JSON 作为主体，我们还可以这样查询姓 Smith 的员工：</p>
<pre><code class="lang-js">GET /megacorp/employee/_search
{
    <span class="hljs-string">"query"</span> : {
        <span class="hljs-string">"match"</span> : {
            <span class="hljs-string">"last_name"</span> : <span class="hljs-string">"Smith"</span>
        }
    }
}
</code></pre>
<p>这个请求会返回同样的结果。你会发现我们在这里没有使用 <em>查询字符串</em>，而是使用了一个由 JSON 构成的请求体，其中使用了 <code>match</code> 查询法，随后我们还将会学习到其他的查询类型。</p>
<h1 id="">更加复杂的搜索</h1>
<p>接下来，我们再提高一点儿搜索的难度。我们依旧要寻找出姓 Smith 的员工，但是我们还将添加一个年龄大于30岁的限定条件。我们的查询语句将会有一些细微的调整来以识别结构化搜索的限定条件 <em>filter（过滤器）</em>:</p>
<pre><code class="lang-js">GET /megacorp/employee/_search
{
    <span class="hljs-string">"query"</span> : {
        <span class="hljs-string">"filtered"</span> : {
            <span class="hljs-string">"filter"</span> : {
                <span class="hljs-string">"range"</span> : {
                    <span class="hljs-string">"age"</span> : { <span class="hljs-string">"gt"</span> : <span class="hljs-number">30</span> } &lt;<span class="hljs-number">1</span>&gt;
                }
            },
            <span class="hljs-string">"query"</span> : {
                <span class="hljs-string">"match"</span> : {
                    <span class="hljs-string">"last_name"</span> : <span class="hljs-string">"Smith"</span> &lt;<span class="hljs-number">2</span>&gt;
                }
            }
        }
    }
}
</code></pre>
<ol>
<li><p>这一部分的语句是 <code>range</code> <em>filter</em> ，它可以查询所有超过30岁的数据 -- <code>gt</code> 代表 <strong>greater than （大于）</strong>。</p>
</li>
<li><p>这一部分我们前一个操作的 <code>match</code> <em>query</em> 是一样的</p>
</li>
</ol>
<p>先不要被这么多的语句吓到，我们将会在之后带你逐渐了解他们的用法。你现在只需要知道我们添加了一个 <em>filter</em>，可以在 <code>match</code> 的搜索基础上再来实现区间搜索。现在，我们的只会显示32岁的名为<strong>Jane Smith</strong>的员工了：</p>
<pre><code class="lang-js">{
   ...
   <span class="hljs-string">"hits"</span>: {
      <span class="hljs-string">"total"</span>:      <span class="hljs-number">1</span>,
      <span class="hljs-string">"max_score"</span>:  <span class="hljs-number">0.30685282</span>,
      <span class="hljs-string">"hits"</span>: [
         {
            ...
            <span class="hljs-string">"_source"</span>: {
               <span class="hljs-string">"first_name"</span>:  <span class="hljs-string">"Jane"</span>,
               <span class="hljs-string">"last_name"</span>:   <span class="hljs-string">"Smith"</span>,
               <span class="hljs-string">"age"</span>:         <span class="hljs-number">32</span>,
               <span class="hljs-string">"about"</span>:       <span class="hljs-string">"I like to collect rock albums"</span>,
               <span class="hljs-string">"interests"</span>: [ <span class="hljs-string">"music"</span> ]
            }
         }
      ]
   }
}
</code></pre>
<h1 id="">全文搜索</h1>
<p>上面的搜索都很简单：名字搜索、通过年龄过滤。接下来我们来学习一下更加复杂的搜索，全文搜索——一项在传统数据库很难实现的功能。
我们将会搜索所有喜欢 <strong>rock climbing</strong> 的员工:</p>
<pre><code class="lang-js">GET /megacorp/employee/_search
{
    <span class="hljs-string">"query"</span> : {
        <span class="hljs-string">"match"</span> : {
            <span class="hljs-string">"about"</span> : <span class="hljs-string">"rock climbing"</span>
        }
    }
}
</code></pre>
<p>你会发现我们同样使用了 <code>match</code> 查询来搜索 <code>about</code> 字段中的 <strong>rock climbing</strong>。我们会得到两个匹配的文档：</p>
<pre><code class="lang-js">{
   ...
   <span class="hljs-string">"hits"</span>: {
      <span class="hljs-string">"total"</span>:      <span class="hljs-number">2</span>,
      <span class="hljs-string">"max_score"</span>:  <span class="hljs-number">0.16273327</span>,
      <span class="hljs-string">"hits"</span>: [
         {
            ...
            <span class="hljs-string">"_score"</span>:         <span class="hljs-number">0.16273327</span>, <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">1</span>&gt;</span>
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         },
         {
            ...
            "_score":         0.016878016, <span class="hljs-tag">&lt;<span class="hljs-title">1</span>&gt;</span>
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}</span>
</code></pre>
<ol>
<li>相关评分</li>
</ol>
<p>通常情况下，Elasticsearch 会通过相关性来排列顺序，第一个结果中，John Smith 的 <code>about</code> 字段中明确地写到 <strong>rock climbing</strong>。而在 Jane Smith 的 <code>about</code> 字段中，提及到了 <strong>rock</strong>，但是并没有提及到 <strong>climbing</strong>，所以后者的 <code>_score</code> 就要比前者的低。</p>
<p>这个例子很好地解释了 Elasticsearch 是如何执行全文搜索的。对于 Elasticsearch 来说，相关性的概念是很重要的，而这也是它与传统数据库在返回匹配数据时最大的不同之处。</p>
<h1 id="">段落搜索</h1>
<p>能够找出每个字段中的独立单词固然很好，但是有的时候你可能还需要去匹配精确的短语或者 <em>段落</em>。例如，我们只需要查询到 <code>about</code> 字段只包含 <strong>rock climbing</strong> 的短语的员工。</p>
<p>为了实现这个效果，我们将对 <code>match</code> 查询变为 <code>match_phrase</code> 查询：</p>
<pre><code class="lang-js">GET /megacorp/employee/_search
{
    <span class="hljs-string">"query"</span> : {
        <span class="hljs-string">"match_phrase"</span> : {
            <span class="hljs-string">"about"</span> : <span class="hljs-string">"rock climbing"</span>
        }
    }
}
</code></pre>
<p>这样，系统会没有异议地返回 John Smith 的文档：</p>
<pre><code class="lang-js">{
   ...
   <span class="hljs-string">"hits"</span>: {
      <span class="hljs-string">"total"</span>:      <span class="hljs-number">1</span>,
      <span class="hljs-string">"max_score"</span>:  <span class="hljs-number">0.23013961</span>,
      <span class="hljs-string">"hits"</span>: [
         {
            ...
            <span class="hljs-string">"_score"</span>:         <span class="hljs-number">0.23013961</span>,
            <span class="hljs-string">"_source"</span>: {
               <span class="hljs-string">"first_name"</span>:  <span class="hljs-string">"John"</span>,
               <span class="hljs-string">"last_name"</span>:   <span class="hljs-string">"Smith"</span>,
               <span class="hljs-string">"age"</span>:         <span class="hljs-number">25</span>,
               <span class="hljs-string">"about"</span>:       <span class="hljs-string">"I love to go rock climbing"</span>,
               <span class="hljs-string">"interests"</span>: [ <span class="hljs-string">"sports"</span>, <span class="hljs-string">"music"</span> ]
            }
         }
      ]
   }
}
</code></pre>
<h1 id="">高亮我们的搜索</h1>
<p>很多程序希望能在搜索结果中 <em>高亮</em> 匹配到的关键字来告诉用户这个文档是 <em>如何</em> 匹配他们的搜索的。在 Elasticsearch 中找到高亮片段是非常容易的。</p>
<p>让我们回到之前的查询，但是添加一个 <code>highlight</code> 参数：</p>
<pre><code class="lang-js">GET /megacorp/employee/_search
{
    <span class="hljs-string">"query"</span> : {
        <span class="hljs-string">"match_phrase"</span> : {
            <span class="hljs-string">"about"</span> : <span class="hljs-string">"rock climbing"</span>
        }
    },
    <span class="hljs-string">"highlight"</span>: {
        <span class="hljs-string">"fields"</span> : {
            <span class="hljs-string">"about"</span> : {}
        }
    }
}
</code></pre>
<p>当我们运行这个查询后，相同的命中结果会被返回，但是我们会得到一个新的名叫 <code>highlight</code> 的部分。在这里包含了 <code>about</code> 字段中的匹配单词，并且会被 <code>&lt;em&gt;&lt;/em&gt;</code> HTML字符包裹住：</p>
<pre><code class="lang-js">{
   ...
   <span class="hljs-string">"hits"</span>: {
      <span class="hljs-string">"total"</span>:      <span class="hljs-number">1</span>,
      <span class="hljs-string">"max_score"</span>:  <span class="hljs-number">0.23013961</span>,
      <span class="hljs-string">"hits"</span>: [
         {
            ...
            <span class="hljs-string">"_score"</span>:         <span class="hljs-number">0.23013961</span>,
            <span class="hljs-string">"_source"</span>: {
               <span class="hljs-string">"first_name"</span>:  <span class="hljs-string">"John"</span>,
               <span class="hljs-string">"last_name"</span>:   <span class="hljs-string">"Smith"</span>,
               <span class="hljs-string">"age"</span>:         <span class="hljs-number">25</span>,
               <span class="hljs-string">"about"</span>:       <span class="hljs-string">"I love to go rock climbing"</span>,
               <span class="hljs-string">"interests"</span>: [ <span class="hljs-string">"sports"</span>, <span class="hljs-string">"music"</span> ]
            },
            <span class="hljs-string">"highlight"</span>: {
               <span class="hljs-string">"about"</span>: [
                  <span class="hljs-string">"I love to go &lt;em&gt;rock&lt;/em&gt; &lt;em&gt;climbing&lt;/em&gt;"</span> &lt;<span class="hljs-number">1</span>&gt;
               ]
            }
         }
      ]
   }
}
</code></pre>
<ol>
<li>在原有文本中高亮关键字。</li>
</ol>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../getting_started/tutorial_indexing.html" class="navigation navigation-prev " aria-label="Previous page: 索引"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../getting_started/tutorial_aggregations.html" class="navigation navigation-next " aria-label="Next page: 汇总"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
